package restaurant.behaviours.kitchen;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import restaurant.agents.KitchenAgent;
import restaurant.core.CustomizedPerformative;
import restaurant.core.Request;

public class KitchenReceiveBehaviour extends CyclicBehaviour {
	
	private static final long serialVersionUID = -4738189588281594178L;
	private KitchenAgent agent;
	
	public KitchenReceiveBehaviour(KitchenAgent a) {
		this.agent = a;
	}

	@Override
	public void action() {
		MessageTemplate messageTemplate = MessageTemplate.MatchPerformative(CustomizedPerformative.WAITER_KITCHEN_COMMUNICATION);
		ACLMessage message = myAgent.receive(messageTemplate);
		if (message != null) {
			// Elabora l'ordine inviato dal cameriere
			String content = "";
			for (int i : this.agent.restaurant.menu.keySet()) {
				content += i + " " + this.agent.restaurant.menu.get(i).toString() + "\n";
			}
			
			this.agent.toCook.push(Request.parseRequest(message.getContent()));
			
			for (int i = 0; i < content.split("\n").length; i++) {
				String[] codici = message.getContent().split(" ");
				for (int j = 1; j < codici.length; j++) {
					if (codici[j].equals(content.split("\n")[i].split(" ")[0])){
						agent.queue.add(content.split("\n")[i].split(" ")[1]);
					}
				}
			}
		}
	}

}